import 'dart:async';

import 'package:fl_extended/fl_extended.dart';
import 'package:flutter/material.dart';
import 'package:system_windows/system_windows.dart';

void main() {
  runApp(MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
          appBar: AppBar(title: const Text('SystemWindows Plugin example')),
          body: const Padding(
            padding: EdgeInsets.all(12),
            child: SingleChildScrollView(child: _App()),
          ))));
}

class _App extends StatefulWidget {
  const _App();

  @override
  State<_App> createState() => _AppState();
}

class _AppState extends State<_App> {
  final windows = SystemWindows();
  TextBox? textBox;
  RecordBox? recordBox;

  String text = '';
  List<String> showText = [
    '我多么希望，有一个门口 早晨，阳光照在草上 我们站着扶着自己的门扇门很低，但太阳是明亮的，草在结它的种子，风在摇它的叶子 我们站着，不说话,就十分美好',
    '我多么希望，有一个门口 早晨，阳光照在草上 我们站着扶着自己的门扇门很低，但太阳是明亮的，草在结它的种子，风在摇它的叶子 我们站着，不说话,就十分美好',
  ];

  @override
  Widget build(BuildContext context) {
    return Column(children: [
      Container(
          height: 60,
          width: double.infinity,
          alignment: Alignment.center,
          padding: const EdgeInsets.all(10),
          decoration:
              BoxDecoration(color: Colors.black12.withValues(alpha: 0.1)),
          child: Text(text)),
      Wrap(runSpacing: 10, spacing: 10, children: [
        ElevatedText(
            text: 'isSupported',
            onPressed: () async {
              final value = await windows.isSupported;
              text = 'isSupported = $value';
              setState(() {});
            }),
        ElevatedText(
            text: 'dispose',
            onPressed: () {
              windows.dispose();
              timer?.cancel();
              timer = null;
            }),
        ElevatedText(
            text: 'toggle front',
            onPressed: () {
              windows.toggle(AppStatus.foreground);
            }),
        ElevatedText(
            text: 'toggle back',
            onPressed: () {
              windows.toggle(AppStatus.background);
            }),
        const Partition('文字弹窗'),
        buildTextBox,
        const Partition('录音弹窗'),
        buildRecordBox,
      ])
    ]);
  }

  Widget get buildTextBox => Wrap(runSpacing: 10, spacing: 10, children: [
        ElevatedText(
            text: 'show',
            onPressed: () async {
              textBox = TextBox(
                  num: showText.length,
                  rect: Rect.fromLTWH(
                      10,
                      kToolbarHeight + context.padding.top + 10,
                      (context.size?.width ?? 300) - 10,
                      200));
              final value = await windows.show(textBox!);
              text = 'show textBox = $value';
              if (value) {
                textBox!.updateText(showText);
              }
              setState(() {});
            }),
        ElevatedText(
            text: 'update text',
            onPressed: () {
              if (timer != null) return;
              timer = 0.3.seconds.timerPeriodic((timer) {
                if (!mounted) {
                  disposeTimer();
                  return;
                }
                var text1 = showText.first;
                var text2 = showText[1];
                text1 += '国';
                text2 += '二';
                if (text1.length > 304) disposeTimer();
                showText[0] = text1;
                showText[1] = text2;
                textBox?.updateText(showText);
              });
            }),
      ]);

  void disposeTimer() {
    timer?.cancel();
    timer = null;
  }

  Timer? timer;

  Widget get buildRecordBox => Wrap(runSpacing: 10, spacing: 10, children: [
        ElevatedText(
            text: 'show',
            onPressed: () async {
              recordBox = RecordBox(
                  rect: Rect.fromLTWH(
                      100, kToolbarHeight + context.padding.top + 10, 300, 300),
                  iconPath: '');
              final value = await windows.show(recordBox!);
              text = 'show recordBox = $value';
              setState(() {});
            }),
        ElevatedText(
            text: 'update time',
            onPressed: () {
              recordBox?.updateTime('00:00:00');
            }),
        ElevatedText(
            text: 'update state',
            onPressed: () {
              recordBox?.updateState(true);
            }),
      ]);

  @override
  void dispose() {
    disposeTimer();
    windows.dispose();
    super.dispose();
  }
}

class Partition extends StatelessWidget {
  const Partition(this.title, {super.key, this.onTap});

  final String title;
  final GestureTapCallback? onTap;

  @override
  Widget build(BuildContext context) => Universal(
      onTap: onTap,
      width: double.infinity,
      color: Colors.grey.withValues(alpha: 0.2),
      alignment: Alignment.center,
      padding: const EdgeInsets.symmetric(vertical: 10),
      margin: const EdgeInsets.symmetric(vertical: 20),
      child: BText(title, fontWeight: FontWeight.bold));
}

class ElevatedText extends StatelessWidget {
  const ElevatedText({super.key, required this.text, required this.onPressed});

  final String text;
  final VoidCallback onPressed;

  @override
  Widget build(BuildContext context) =>
      ElevatedButton(onPressed: onPressed, child: Text(text));
}
